package net.awpspace.mobile.form;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.global.Client;
import net.awpspace.mobile.global.Database;
import net.awpspace.mobile.global.Global;
import net.awpspace.mobile.global.Utils;

import com.sun.lwuit.Button;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.table.TableLayout;

/**
 * Login main form
 * 
 * @author Diep
 * 
 */
public class MainForm extends BaseForm {

	private Command cmdLogin;
	private Command cmdRegister;

	private TextField username;
	private TextField password;
	private TextField hostname;
	private CheckBox isLocal;

	public MainForm(MIDlet app, BaseForm parent) {
		super(app, parent);
		setTitle(Global.NAME_GROUP);
		setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		
		username = new TextField(1);
		password = new TextField(1);
		password.setConstraint(TextField.PASSWORD);
		hostname = new TextField(1);
		hostname.setEnabled(false);
		hostname.setText(Global.HOST_NAME);
		isLocal = new CheckBox("Local Storage");
		isLocal.setSelected(true);
		isLocal.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				hostname.setEnabled(!isLocal.isSelected());
				Global.isLocal = isLocal.isSelected();
			}
		});

		TableLayout layout = new TableLayout(3, 2);
		TableLayout.Constraint c = layout.createConstraint();
		c.setWidthPercentage(60);

		Container ctnLoginForm = new Container(layout);
		ctnLoginForm.addComponent(new Label("Username:"));
		ctnLoginForm.addComponent(c, username);
		ctnLoginForm.addComponent(new Label("Password:"));
		ctnLoginForm.addComponent(password);

		cmdLogin = new Command("Login");
		cmdRegister = new Command("Register");
		Container ctnLoginButton = new Container(new GridLayout(1, 2));
		ctnLoginButton.addComponent(new Button(cmdLogin));
		ctnLoginButton.addComponent(new Button(cmdRegister));

		addComponent(ctnLoginForm);
		addComponent(isLocal);
		addComponent(new Label("Server:"));
		addComponent(hostname);
		addComponent(ctnLoginButton);
	}

	protected void processCommand(ActionEvent e) {
		Client.hostName = hostname.getText();
		if (e.getCommand().equals(cmdLogin)) {
			login(username.getText(), password.getText());
		} else if (e.getCommand().equals(cmdRegister)) {
			register(username.getText(), password.getText());
		}
	}

	private void login(String username, String password) {
		boolean result = Global.isLocal ? Database.getInstance().checkLogin(
				username, password) : Client.getInstance().login(username,
				password);
		if (result) {
			new MenuForm(app, this).show();
		} else {
			Utils.getInstance().alert("Username and password do not match!");
		}
	}

	private void register(String username, String password) {
		Utils.getInstance().alert(
				(Global.isLocal ? Database.getInstance().checkRegister(
						username, password) : Client.getInstance().register(
						username, password)) ? "Registered!"
						: "Username already exists!");
	}
}
